import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

class SearchResultWidget extends StatelessWidget {
  final String content;
  final Color? highlightColor;
  final TextStyle? textStyle;

  const SearchResultWidget({
    Key? key,
    required this.content,
    this.highlightColor,
    this.textStyle,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (_isImageUrl(content)) {
      return _buildImageResult();
    } else {
      return _buildTextResult();
    }
  }

  /// 判断是否为图片链接
  bool _isImageUrl(String url) {
    final imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp'];
    final lowerUrl = url.toLowerCase();
    
    // 检查是否包含图片扩展名
    bool hasImageExtension = imageExtensions.any((ext) => lowerUrl.contains(ext));
    
    // 检查是否为网络图片链接
    bool isNetworkImage = (lowerUrl.startsWith('http://') || lowerUrl.startsWith('https://')) && hasImageExtension;
    
    // 检查是否为本地图片路径
    bool isLocalImage = !lowerUrl.startsWith('http') && hasImageExtension;
    
    return isNetworkImage || isLocalImage;
  }

  /// 构建文字搜索结果
  Widget _buildTextResult() {
    final defaultTextStyle = textStyle ?? const TextStyle(
      fontSize: 16,
      color: Colors.black87,
    );
    
    final highlightTextStyle = defaultTextStyle.copyWith(
      color: highlightColor ?? Colors.black,
      fontWeight: FontWeight.bold,
    );

    return RichText(
      text: TextSpan(
        style: defaultTextStyle,
        children: [
          const TextSpan(text: 'Search result for '),
          TextSpan(
            text: content,
            style: highlightTextStyle,
          ),
        ],
      ),
    );
  }

  /// 构建图片搜索结果
  Widget _buildImageResult() {
    final defaultTextStyle = textStyle ?? const TextStyle(
      fontSize: 16,
      color: Colors.black87,
    );

    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          'Search result ',
          style: defaultTextStyle,
        ),
        const SizedBox(width: 8),
        _buildImage(),
      ],
    );
  }

  /// 构建图片组件
  Widget _buildImage() {
    if (content.startsWith('http://') || content.startsWith('https://')) {
      // 网络图片
      return CachedNetworkImage(
        imageUrl: content,
        width: 40,
        height: 40,
        fit: BoxFit.cover,
        placeholder: (context, url) => Container(
          width: 40,
          height: 40,
          decoration: BoxDecoration(
            color: Colors.grey[200],
            borderRadius: BorderRadius.circular(4),
          ),
          child: const Icon(
            Icons.image,
            color: Colors.grey,
            size: 20,
          ),
        ),
        errorWidget: (context, url, error) => Container(
          width: 40,
          height: 40,
          decoration: BoxDecoration(
            color: Colors.grey[200],
            borderRadius: BorderRadius.circular(4),
          ),
          child: const Icon(
            Icons.broken_image,
            color: Colors.grey,
            size: 20,
          ),
        ),
      );
    } else {
      // 本地图片
      return ClipRRect(
        borderRadius: BorderRadius.circular(4),
        child: Image.asset(
          content,
          width: 40,
          height: 40,
          fit: BoxFit.cover,
          errorBuilder: (context, error, stackTrace) {
            return Container(
              width: 40,
              height: 40,
              decoration: BoxDecoration(
                color: Colors.grey[200],
                borderRadius: BorderRadius.circular(4),
              ),
              child: const Icon(
                Icons.broken_image,
                color: Colors.grey,
                size: 20,
              ),
            );
          },
        ),
      );
    }
  }
}

/// 使用示例：
/// 
/// // 文字搜索结果
/// SearchResultWidget(
///   content: '苹果手机',
///   highlightColor: Colors.orange,
/// )
/// 
/// // 图片搜索结果
/// SearchResultWidget(
///   content: 'https://example.com/image.jpg',
/// )